package demo;

import demo.config.TimerConfig;
import demo.config.WebConfig;
import org.eclipse.jetty.server.*;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

//class FirstHandler extends AbstractHandler {
//	public void handle(String target,
//	                   Request baseRequest,
//	                   HttpServletRequest request,
//	                   HttpServletResponse response) throws IOException,
//			ServletException {
//		// Declare response encoding and types
//		response.setContentType("text/html; charset=utf-8");
//
//		// Declare response status code
//		response.setStatus(HttpServletResponse.SC_OK);
//
//		// Write back response
//		response.getWriter().println("<h1>Hello World</h1>");
//
//		// Inform jetty that this request has now been handled
//		baseRequest.setHandled(true);
//	}
//}

public class AppMain {

	private static ServletContextHandler springHandler(WebApplicationContext applicationContext) {
		ServletContextHandler servletContextHandler = new ServletContextHandler();
		servletContextHandler.setContextPath("/");
		DispatcherServlet dispatcherServlet = new DispatcherServlet();
		dispatcherServlet.setApplicationContext(applicationContext);
		dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
		servletContextHandler.addServlet(new ServletHolder(dispatcherServlet), "/*");
		return servletContextHandler;
	}

	public static void startSingleServer(int port) throws Exception {
		Server server = new Server(port);

		AnnotationConfigWebApplicationContext spring = new AnnotationConfigWebApplicationContext();
		spring.register(WebConfig.class);
		spring.register(TimerConfig.class);
		server.setHandler(springHandler(spring));

		try {
			server.start();
		} catch (Exception e) {
			if (server.isFailed()) {
				server.stop();
			}
		}
		server.join();
	}

	public static void startMultiThreadServer(int port) throws Exception {
		QueuedThreadPool threadPool = new QueuedThreadPool(256, 16, 5000);
		threadPool.setStopTimeout(5000);
		Server server = new Server(threadPool);

		server.setStopAtShutdown(true);
		server.setStopTimeout(60000);

		ServerConnector connector = new ServerConnector(server);
		connector.setPort(port);
		connector.setStopTimeout(30000);
		server.setConnectors(new Connector[]{connector});

		AnnotationConfigWebApplicationContext spring = new AnnotationConfigWebApplicationContext();
		spring.register(WebConfig.class);
		spring.register(TimerConfig.class);
		server.setHandler(springHandler(spring));

		try {
			server.start();
		} catch (Exception e) {
			if (server.isFailed()) {
				server.stop();
			}
		}
	}

	public static void main(String[] args) throws Exception {
		startMultiThreadServer(8001);
	}
}